package com.lianniu.mgr.demand.controller;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.dict.model.DictionaryVo;
import com.lianniu.mgr.baseService.dict.service.IDictionaryService;
import com.lianniu.mgr.demand.service.IDemandService;
import com.lianniu.mgr.demand.vo.*;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.service.IProductSpecValueService;
import com.lianniu.mgr.util.ObjectUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by Administrator on 2017-05-14.
 */
@Controller
@RequestMapping("/rest/demand")
public class DemandRestController {
    private static final Logger logger = Logger.getLogger(DemandRestController.class);
    @Autowired
    private IDemandService demandService;
    @Autowired
    IDictionaryService dictionaryService;
    @Autowired
    private IProductSpecValueService psvService;

    @RequestMapping(value="/demandList",method={RequestMethod.GET,RequestMethod.POST})
    public @ResponseBody  BaseResult getDemandList(HttpServletRequest req, DemandConditionVo condition, PaginationInfo paginationInfo){
        logger.info("getDemandList begin");
        String demandCode = req.getParameter("demandCode");
        String isOutDate = req.getParameter("isOutDate");
        String productName = req.getParameter("productName");
        String approveStatus = req.getParameter("approveStatus");
        String userName = req.getParameter("userName");

        BaseResult demand = null;
        if(!"".equals(demandCode) || !"".equals(isOutDate)||!"".equals(productName)||!"".equals(approveStatus)||!"".equals(userName)){
            condition.setDemandCode(demandCode);
            condition.setIsOutDate(isOutDate);
            condition.setProductName(productName);
            condition.setApproveStatus(approveStatus);
            condition.setUserName(userName);
        }
        logger.info("data:"+condition);
        demand = demandService.getDemandListForPage(condition, paginationInfo);
        logger.info("params[需求单数据]"+ JSON.toJSONString(demand));
        return demand;
    }
    @RequestMapping("/getDemand")
    public @ResponseBody BaseResult getDemand(String demandId,ModelMap map){
        BaseResult demand = demandService.getDemandById(demandId);
        DictionaryVo dic = new DictionaryVo();
        dic.setTypeName("demandapproveStatus");
        BaseResult approveStatus= dictionaryService.selectDictionaryByDicType(dic);
        map.put("approveStatus", approveStatus.getData());

        ObjectUtil.nvlProperty(demand.getData());
        map.put("demand", demand);

        return BaseResult.success(map);
    }

    @RequestMapping("/delDemand")
    public @ResponseBody BaseResult delDemand( String demandId){
        logger.info(String.format("delDemand %s Demand", demandId));
        BaseResult result = demandService.updateDemandDelFlag(demandId);
        logger.info("delDemand Demand result ： "+result);
        return result;
    }


    @RequestMapping(value="/modifyDemand",method={RequestMethod.POST})
    public @ResponseBody BaseResult modifyOrder(DemandVo demandVo,DemandDetailVo demandDetailVo){
        BaseResult result = demandService.modifyDemand(demandVo,demandDetailVo);
        return BaseResult.success(result);
    }

    @RequestMapping(value="/getCateId",method={RequestMethod.GET})
    public @ResponseBody BaseResult getCateId(){
        BaseResult result = psvService.selectById("77");
        return BaseResult.success(result);
    }

    @RequestMapping(value="/demandSuppyList",method={RequestMethod.GET,RequestMethod.POST})
    public @ResponseBody  BaseResult demandSuppyList(HttpServletRequest req, DemandSupplyConditionVo condition, PaginationInfo paginationInfo){
        logger.info("demandSuppyList begin");
       String demandId = req.getParameter("demandId");

        BaseResult demandSupply = null;
        BaseResult demand = null;
        if(!"".equals(demandId)){
            condition.setDemandId(demandId);
        }
        logger.info("data:"+condition);
        demandSupply = demandService.getDemandSupplyListForPage(condition, paginationInfo);
        logger.info("params[报价单数据]"+ JSON.toJSONString(demandSupply));
        return demandSupply;
    }

    @RequestMapping("/delDemandSupply")
    public @ResponseBody BaseResult delDemandSupply( String id){
        logger.info(String.format("delDemandSupply %s DemandSupply", id));
        BaseResult result = demandService.updateDemandSupplyDelFlag(id);
        logger.info("delDemandSupply DemandSupply result ： "+result);
        return result;
    }

    @RequestMapping("/getDemandSupply")
    public @ResponseBody BaseResult getDemandSupply(String id,ModelMap map){
        BaseResult demandSupply = demandService.getDemandSupplyById(id);
        ObjectUtil.nvlProperty(demandSupply.getData());
        map.put("demandSupply", demandSupply);
        return BaseResult.success(map);
    }

    @RequestMapping(value="/modifyDemandSupply",method={RequestMethod.POST})
    public @ResponseBody BaseResult modifyDemandSupply(DemandSupplyVo demandSupplyVo){
        BaseResult result = demandService.modifyDemandSupply(demandSupplyVo);
        return BaseResult.success(result);
    }

}

